/*
 * Copyright (C) 2008  YATR.Main@gmail.com
 *
 * $Id$
 * You may distribute under the GPL-2 License , as specified in 
 * the README.txt file.
 *
 * For more information about YATR, or for information on how to 
 * contact the author, see the README.txt file.
 */

package org.yatr.tci;

import org.etsi.ttcn.tci.BooleanValue;
import org.etsi.ttcn.tci.TciTypeClass;
import org.etsi.ttcn.tci.Value;

import static org.yatr.tci.Variable.*;

/**
 * @author YATR.Main@gmail.com
 *
 */
public class BooleanValueYatr extends Variable implements BooleanValue {

	public BooleanValueYatr() {
		mValue = false;
	}
	
	public BooleanValueYatr(Object o) {
		this.assign(o);
	}
	
	/* (non-Javadoc)
	 * @see org.yatr.tci.Variable#assign(java.lang.Object[])
	 */
	@Override
	public void assign(Object... objs) {
		checkObjectArray(objs);
		
		Object o = objs[0];
		if(o instanceof BooleanValueYatr) {
			BooleanValueYatr o2 = (BooleanValueYatr)o;
			checkOmit(o2);
			mValue = o2.mValue;
		} else if(o instanceof Boolean) {
			mValue = (Boolean)o;
		} else {
			checkTypeException(o);
		}
		setIsOmit(false);
	}

	/* (non-Javadoc)
	 * @see org.yatr.tci.Variable#encode(org.yatr.tci.BitBuffer)
	 */
	@Override
	public void encode(BitBuffer buf) {
		checkNull(buf);
		checkOmit(this);
		
		buf.writeNumber(mValue?1:0, 1);
	}

	/* (non-Javadoc)
	 * @see org.yatr.tci.Variable#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object o) {
		checkNull(o);
		checkOmit(this);
		if(o instanceof BooleanValueYatr) {
			BooleanValueYatr o2 = (BooleanValueYatr)o;
			checkOmit(o2);
			return mValue == o2.mValue;
		} else if(o instanceof Boolean) {
			return mValue == (Boolean)o;
		} else {
			checkTypeException(o);
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see org.yatr.tci.Variable#toString()
	 */
	@Override
	public String toString() {
		if(getIsOmit())
			return UNDEFINED;
		
		if(mValue)
			return "true";
		else
			return "false";
	}

	/* (non-Javadoc)
	 * @see org.etsi.ttcn.tci.BooleanValue#getBoolean()
	 */
	public boolean getBoolean() {
		checkOmit(this);
		return mValue;
	}

	/* (non-Javadoc)
	 * @see org.etsi.ttcn.tci.BooleanValue#setBoolean(boolean)
	 */
	public void setBoolean(boolean value) {
		mValue = value;
		setIsOmit(false);
	}

	/* (non-Javadoc)
	 * @see org.etsi.ttcn.tci.Type#getName()
	 */
	public String getName() {
		return "boolean";
	}

	/* (non-Javadoc)
	 * @see org.etsi.ttcn.tci.Type#getTypeClass()
	 */
	public int getTypeClass() {
		return TciTypeClass.BOOLEAN;
	}

	/* (non-Javadoc)
	 * @see org.etsi.ttcn.tci.Type#newInstance()
	 */
	public Value newInstance() {
		return new BooleanValueYatr();
	}
	
	/**
	 * e.g. a == b
	 * @return
	 */
	public BooleanValueYatr equalsOp(Object o) {
		return new BooleanValueYatr(this.equals(o));
	}
	
	public BooleanValueYatr notEqualsOp(Object o) {
		return new BooleanValueYatr(!this.equals(o));
	}

	boolean mValue;
}

class MyBoolean extends BooleanValueYatr {
	public MyBoolean() {}
	public MyBoolean(Object o) { super(o);}
	//override
	public String getName() {
		return "MyBoolean";
	}
	public Value newInstance() {
		return new MyBoolean();
	}
}

